<?php
/**
 * 点赞
 */
namespace Common\Model;

class PraiseModel extends BaseModel{
	private static $_instance=null;
	private static function getInstance(){
		if(!self::$_instance){
			self::$_instance=M('praise_log',null,DB_MAIN_CFG);
		}
		return self::$_instance;
	}

	/**
	 * 点赞操作
	 * @param $objType
	 * @param $objID
	 * @param $uid
	 * @param $nickname
	 * @param int $flag
	 * @return array
	 */
	public static function praise($objType,$objID,$uid,$nickname,$flag=1){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
		$dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
		if(!$uid){
			return $dataFailure;
		}
		$now=time();
		$praiseM=self::getInstance();
		//确定操作表
		$table=C('OBJ_TABLE');
		$table=$table[$objType];

		if($flag){	//如果是评论的点赞，对象类型为0
			$contentType = M($table,null,DB_MAIN_CFG)->where("id={$objID}")->getField("contentType");
		} else{
			$contentType = 0;
		}

		$newData=array(
			'objID'				=>$objID,
			'objType'			=>$objType,
			'objContentType'	=>$contentType,
			'uid'				=>$uid,
			'nickname'			=>$nickname,
			'addTime'			=>$now,
			'praiseNum' 		=>1,
		);


		$where="objType={$objType} and objID={$objID} and uid={$uid}";

		if($praiseM->where($where)->find()){
			if($flag == 0){
				//暂不支持评论点赞的取消点赞
				$dataFailure['msg'] = "已点赞";
				return $dataFailure;
			}
		}else{
			$res=$praiseM->add($newData);
			if($res===false){
				$dataFailure['msg']='点赞数据插入失败';
				return $dataFailure;
			}
			M($table,null,DB_MAIN_CFG)->where("id={$objID}")->setInc('praiseNum');

			if($objType==OBJ_TYPE_ARTICLE){
				//发布通知消息
				$articleInfo=M('article',null,DB_MAIN_CFG)
					->where("id={$objID}")
					->field('uid,title')
					->find();
				NoticeModel::saveNotice(NoticeModel::NOTICE_TYPE_PRAISE,$objType,$objID,$uid,$articleInfo['uid'],$articleInfo['title']);
			}

			//点赞加积分
			$valRet=UserModel::addUserValByAction($uid,OPT_PRAISE );
			if($valRet['status']==0){
				$valRet['tip']=S('tip_'.$uid);
				S('tip_'.$uid,null);
				return $valRet;
			}
			if($valRet['tip']){
				$dataSuccess['msg'] = $valRet['tip'];
			}
			
		}
		return $dataSuccess;
	}

	/**
	 * 对于评论的点赞操作
	 * Author:wwl
	 */
	public static function commentPraise($objType,$objID,$uid,$nickname){

	}


	/**
	 * 获取对象点赞信息，
	 * Author: shijy
	 * @param $objType
	 * @param $objId
	 * @param int $uid
	 * @return array(
			count:点赞数
	        done:指定uid有无进行点赞，0否，1是，2未知
	 * )
	 */
	public static function getPraiseState($objType,$objId,$uid=0){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
		$dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
		if(!$objType || !$objId){
			return $dataFailure;
		}
		$praiseM=self::getInstance();

		$done=0;
		if($uid){
			$done=$praiseM->where('objType='.$objType.' and objID='.$objId.' and uid='.$uid)->find()?1:0;
		}
		$count=$praiseM->where('objType='.$objType.' and objID='.$objId )->count();
		$dataSuccess['done']=$done;
		$dataSuccess['count']=$count;
		return $dataSuccess;
	}

	/**
	 * 获取点赞列表数据
	 * Author: shijy
	 * @param $objType 对象类型
	 * @param $objId 对象编号
	 */
	public static function getPraiseList($objType,$objId,$page,$pageSize=10){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
		$dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
		if(!$objType || !$objId){
			return $dataFailure;
		}
		$praiseM=self::getInstance();
		$rowCount=$praiseM->where('objType='.$objType.' and objID='.$objId )->count();

		$pageCount=max(1,ceil($rowCount/$pageSize));
		$page=max($page,1);
		$offset=($page-1)*$pageSize;
		$list=$praiseM->where('objType='.$objType.' and objID='.$objId)->order('id desc')->limit($offset,$pageSize)->select();

		$dataSuccess['rowCount']=$rowCount;
		$dataSuccess['pageCount']=$pageCount;
		$dataSuccess['page']=$page;
		$dataSuccess['list']=$list;
		$dataSuccess['totalPage']=$pageCount;
		$dataSuccess['nextPage'] = $dataSuccess['page'] >= $dataSuccess['totalPage'] ? 0 : 1;//是有有下一页
		return $dataSuccess;
	}


}